package com.job.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.job.common.Constant;
import com.job.common.ResponseResult;
import com.job.common.asserts.MyAssert;
import com.job.common.enums.ResultCodeEnum;
import com.job.common.exception.ParameterIsNullException;
import com.job.controller.req.ChatRecordsVO;
import com.job.controller.req.ReadMessageVO;
import com.job.controller.resp.UnreadRespVO;
import com.job.dao.MessageDao;
import com.job.mapper.MessageMapper;
import com.job.mapper.UserMapper;
import com.job.pojo.Message;
import com.job.pojo.User;
import com.job.pojo.dto.UnreadDTO;
import com.job.service.MessageService;
import com.job.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class MessageServiceImpl implements MessageService, Constant {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private MessageDao messageDao;

    @Override
    public ResponseResult<ArrayList<UnreadRespVO>> QueryUnreadList(Integer userId) throws
             ParameterIsNullException {
        MyAssert.objectParameterCheck(userId);

        List<UnreadDTO> unreadDTOS = messageMapper.unreadCount(userId);
        ArrayList<UnreadRespVO> unreadRespVOS = new ArrayList<UnreadRespVO>();
        unreadDTOS.forEach(unreadDTO -> {
            UnreadRespVO unreadRespVO = new UnreadRespVO();
            User user = userMapper.selectById(unreadDTO.getFromId());
            Message message = messageMapper.lastMessage(unreadDTO.getFromId(), userId);
            BeanUtils.copyProperties(user,unreadRespVO);
            unreadRespVO.setLastMessage(message.getContext());
            unreadRespVO.setUserId(unreadDTO.getFromId());
            unreadRespVOS.add(unreadRespVO);
        });

        return new ResponseResult<ArrayList<UnreadRespVO>>().success(unreadRespVOS);
    }

    @Override
    public ResponseResult<ResultCodeEnum> read(ReadMessageVO messageVO) throws ParameterIsNullException {
        MyAssert.objectParameterCheck(messageVO);
        if (messageMapper.read(messageVO) < 1){
            new ResponseResult<ResultCodeEnum>().fail(ResultCodeEnum.FAIL);;
        }

        return new ResponseResult<ResultCodeEnum>().success();
    }

    @Override
    public ResponseResult<Page<Message>> queryChatRecords(ChatRecordsVO chatRecordsVO) {
        String conversationId = MessageUtil.getConversationId(chatRecordsVO.getFromId(), chatRecordsVO.getToId());
        Page<Message> messagePage = messageDao.queryChatRecordsList(conversationId, chatRecordsVO);
        if (CollectionUtils.isEmpty(messagePage.getRecords())){
            return new ResponseResult<Page<Message>>().fail(ResultCodeEnum.MESSAGE_REDRODS_IS_NULL);
        }

        return new ResponseResult<Page<Message>>().success(messagePage);
    }
}
